﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GpuStructCompiler
{
    public class SyntaxNode
    {
    }
    public class VariableDeclare : SyntaxNode
    {
        public string TypeName { get; set; }
        public string Name { get; set; }
    }
    public class StructDeclare : SyntaxNode
    {
        public string Name { get; set; }
        public List<VariableDeclare> Members { get; set; }
        
        public StructDeclare()
        {
            Members = new List<VariableDeclare>();
        }
    }
    public class Document : SyntaxNode
    {
        public List<StructDeclare> Structs { get; set; }
        public Document()
        {
            Structs = new List<StructDeclare>();
        }

        public void Expand(StructDeclare s, List<VariableDeclare> plainMembers, List<VariableDeclare> groups, string groupPrefix)
        {
            groups.Add(new VariableDeclare() { TypeName = s.Name, Name = groupPrefix });
            foreach (var member in s.Members)
            {
                StructDeclare sd = GetStructDeclare(member.TypeName);
                if (sd != null)
                {
                    Expand(sd, plainMembers, groups, groupPrefix + "_" + member.Name);
                }
                else
                {
                    plainMembers.Add(new VariableDeclare(){ TypeName = member.TypeName, Name = groupPrefix + "_" + member.Name});
                }
            }
        }

        private StructDeclare GetStructDeclare(string p)
        {
            return Structs.Find(s => s.Name == p);
        }
    }
}
